package com.niiwoo.activity.stage.enums.mall;

import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Date;

/**
 * @author zzl
 * @create 2018-10-13 11:13
 * @desc banner上下架状态
 **/
@Getter
@AllArgsConstructor
public enum BannerShelvesStatusEnum {
    WAIT(Byte.valueOf("1"), "待上架"),
    UP(Byte.valueOf("2"), "上架中"),
    DOWN(Byte.valueOf("3"), "已过期");

    private Byte code;
    private String desc;

    /**
     * 根据时间获取上下架状态
     * @param upShelvesTime
     * @param downShelvesTime
     * @return
     */
    public static BannerShelvesStatusEnum valueOf(Date upShelvesTime, Date downShelvesTime){
        //没限制上下架时间的，默认为已上架
        if(upShelvesTime == null || downShelvesTime == null){
            return BannerShelvesStatusEnum.UP;
        }
        Date now = new Date();
        if(upShelvesTime.after(now)){
            return BannerShelvesStatusEnum.WAIT;
        }
        if(downShelvesTime.before(now)){
            return BannerShelvesStatusEnum.DOWN;
        }
        if(now.after(upShelvesTime)&&now.before(downShelvesTime)){
            return BannerShelvesStatusEnum.UP;
        }
        //出现这种情况，就是上架时间>下架时间（业务上不允许这种情况），和时间刚好在临界值，返回up
        return BannerShelvesStatusEnum.UP;
    }

    public final static BannerShelvesStatusEnum valueOfCode(Byte code){
        for(BannerShelvesStatusEnum item :values()){
            if(item.getCode().equals(code)){
                return item;
            }
        }
        return null;
    }
}
